// pages/02admin/02tableClass/tableClass.js
var app = getApp()
var appData = app.globalData
var arr1 = [];
var co = require('../../js/common.js')
var count = 0

Page({
   data: {
      mainx: 0,
      printerList: [],
      isShowInputLabelArr: [0, 0],
      tableCateList: null,
      edit_id: -1,
      pick_i: 0,
      mode: '',
      exchange_id: -1,
      buttonText: "新增餐桌类型",
      sorting: '编辑排序',
      tempIndex: '',
   },

   sorting: function () {
      this.setData({
         mode: this.data.mode == '' ? 'sort' : '',
         sorting: this.data.mode == 'sort' ? '编辑排序' : '返回',
      })
   },
   editTableCate: function () {
      this.setData({
         pick_i: 0,
         mode: this.data.mode == '' ? 'add' : '',
         buttonText: this.data.mode == '' ? '取消新增' : '新增餐桌类型',
         edit_id: -1,
         isShowInputLabelArr: [0, 0]
      })
   },

   onLoad: function (options) {
      this.setData({
         tableCateList: appData.tableCateArr
      })
      arr1 = appData.tableCateArr

      console.log('this.data.tableCateList', this.data.tableCateList)
      console.log('getApp().globalData.tableCateArr', getApp().globalData.tableCateArr)
   },

   //编辑框
   editTable: function (e) {
      var td = this.data
      var index = e.currentTarget.id
      var printer = td.printerList[index]
      this.setData({
         edit_id: td.edit_id == index ? -1 : index,
         isShowInputLabelArr: index > -1 ? [1, 1] : [0, 0],
         mode: ''
      })
      // console.log('this.data233333333333==', td)
   },

   //餐桌分类排序
   toExchange: function (e) {
      var td = this.data
      var t = this
      var index = e.currentTarget.id
      var idx
      if(count%2 == 0){
         t.setData({ tempIndex: index})
         count = 1
      }else{
         idx = e.currentTarget.id
         count = 0
      }
      if (td.exchange_id != -1 && index != '') {
         var param = {
            Biz_ID: appData.shopInfo.Biz_ID,
            Table_Cate_ID1: appData.tableCateArr[t.data.tempIndex].cateId,
            Table_Cate_ID2: appData.tableCateArr[idx].cateId
         }
         app.getJsonData('table/Tmg/exchangeTableCate', param, function (res) {
            t.setData({
               tableCateList: res.data,
               exchange_id: -1
            })
            appData.tableCateArr = res.data
         })
      } else {
         this.setData({ exchange_id: index == '' ? -1 : index })
      }

   },
   //添加或修改
   submitTableCate: function (e) {
      var td = this.data
      var t = this
      var apiURL
      var list = td.tableCateList
      var param = e.detail.value
      param.Biz_ID = appData.shopInfo.Biz_ID
      //判断更新和插入 
      if (td.edit_id > -1) {
         param.cateId = list[td.edit_id].cateId
         param.cateSort = list[td.edit_id].cateSort
         apiURL = 'table/Tmg/updateTableCate'
      } else {
         apiURL = 'table/Tmg/insertTableCate'
      }
      app.getJsonData(apiURL, param, function (res) {
         console.log('【添加或修改返回信息】', res.msg)
         if (td.edit_id > -1) {
            list[td.edit_id] = res.data
         } else {
            list.push(res.data)
         }
         t.setData({
            edit_id: -1,
            tableCateList: list,
            isShowInputLabelArr: [0, 0],
            mode: '',
            buttonText: "新增餐桌类型"
         })
         appData.tableCateArr = list
         //通知更新
         // app.noticeUpdateTableList(res.data)
      })
   },
   //整理排序
   arrange: function (e) {
      var td = this.data
      var t = this
      var param = {
         shop_id: appData.shopInfo.Biz_ID
      }
      //tp5
      app.getNetData('table/cate/sortOut', param, function (res) {
         t.setData({ tableCateList: res.data })
         appData.tableCateArr = res.data
      })
   },
   // 删除餐桌分类
   delTable: function (e) {
      var t = this
      app.confirm('重要提示', '确认删除吗？','取消', '删除', function (res) {
         console.log('delConfirm: function', res)
         if (res) {
            t.delTableCate(e)
         }
      })
   },
   delTableCate: function (e) {
      if (app.globalData.loginMode == 'boss') {
         var i = e.currentTarget.id
         var list = this.data.tableCateList

         var param = {
            Biz_ID: appData.shopInfo.Biz_ID,
            cateSort: list[i].cateSort
         }
         var that = this
         app.getJsonData('table/Tmg/deleteTableCate', param, function (res) {
            console.log('table_cate_sav:res', res)
            if (res.code == 0) {
               list.splice(i, 1)
               that.setData({
                  tableCateList: list
               })
               // res.data.notice = 'del'
               // app.noticeUpdateTableCateList(res.data)//通知删除更新
            } else {
               wx.showModal({
                  title: '删除失败！',
                  content: res.message
               })
            }
         })
      }
      else {
         wx.showToast({
            title: '需要掌柜登录才能操作',
         })
      }
   },

   showInputLabel: function (e) {
      if (e.detail.value.length > 1) return;
      var index = Number(e.currentTarget.id);
      var isShowArray = this.data.isShowInputLabelArr;
      isShowArray[index] = true;

      this.setData({
         isShowInputLabelArr: isShowArray
      })
      // console.log(isShowArray);
      // console.log(this.data.isShowInputLabelArr);
      // console.log(e.currentTarget.id);
   },

})

